<!--
 * @Date: 2023-10-05 18:00:07
 * @LastEditors: admin@54xavier.cn
 * @LastEditTime: 2024-12-15 02:48:55
 * @FilePath: /electron-hiprint/assets/print.html
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
      <title>打印窗口</title>
      <link href="./print-lock.css" rel="stylesheet" />
      <link href="./print-lock.css" media="print" rel="stylesheet" />
  <style>
    @page {
      border: 0;
      padding: 0;
      margin: 0;
      size: portrait !important;
    }
    body {
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      /*overflow: hidden;*/
      /*width: 180mm;*/
    }
  </style>
  <script>
    window.$ = window.jQuery = require("jquery");
    $.fn.onImgLoaded = (callback) => {
      let cb = (len) => {
        if (len <= 0) {
          callback();
        }
      };
      let len = $("img").length;
      cb(len);
      let getUrl = (str) => {
        let reg = /(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;
        let v = str.match(reg);
        if (v && v.length) {
          return v[0];
        }
        return "";
      };
      $("img").each((i, e) => {
        let $img = $(e);
        let img = new Image();
        let src = $img.attr("src");
        if (!new RegExp("[a-zA-Z]+://[^\s]*").test(src)) {
          src = getUrl($img.attr("style") || "");
        }
        img.src = src;
        if (img.complete || src == "") {
          len--;
          cb(len);
        } else {
          img.onload = () => {
            len--;
            cb(len);
          };
          img.onerror = () => {
            len--;
            cb(len);
          };
        }
      });
    };
  </script>
</head>

<body>
<div id="printElement"></div>
<script>
  const {ipcRenderer} = require("electron");

  $(document).ready(function () {
    ipcRenderer.on("print-new", (event, data) => {
      document.title = data.title ? data.title : "小票打印";
      document.getElementById("printElement").innerHTML = data.html;
      data.pageNum = $(".hiprint-printPaper").length;
      $.fn.onImgLoaded(() => {
        ipcRenderer.send("do", data);
      });
    });
    ipcRenderer.on("reprint", (event, data) => {
      document.title = data.title ? data.title : "hiprint打印";
      document.getElementById("printElement").innerHTML = data.html;
      $.fn.onImgLoaded(() => {
        ipcRenderer.send("do", data);
      });
    });
  });
</script>
</body>
</html>
